import Toast from 'tdesign-miniprogram/toast/index';
import request from '../../../../utils/request';

Page({
  externalClasses: ['theme-wrapper-class'],
  data: {
    id: 0,
    originFiles: [

    ],
    successNum: 0,
    evaImages: [],
    gridConfig: {
      column: 4,
      width: 160,
      height: 160,
    },
    config: {
      count: 1,
    },
  },
  privateData: {
    verifyTips: '',
  },
  onLoad(options) {
    const { id } = options;
    console.log("dd")
    this.setData({ id: id });
  },

  handleSuccess(e) {
    const { files } = e.detail;
    this.setData({
      originFiles: files,
    });
  },
  handleRemove(e) {
    const { index } = e.detail;
    const { originFiles } = this.data;
    originFiles.splice(index, 1);
    this.setData({
      originFiles,
    });
  },
  handleClick(e) {
    console.log(e.detail.file);
  },

  hasSava: false,



  formSubmit() {
    var that = this;
    const { submitActive, originFiles } = this.data;
    // const { isLegal, tips } = this.onVerifyInputLegal();
    // if (!isLegal) {
    //   Toast({
    //     context: this,
    //     selector: '#t-toast',
    //     message: tips,
    //     icon: '',
    //     duration: 1000,
    //   });
    //   return;
    // }
    //upload image
    if (originFiles.length > 0) {
      originFiles.forEach(file => this.uploadFile(file))
    } else {
      this.saveEva();
    }
  },
  uploadFile(file) {
    var that = this;
    wx.compressImage({
      src: file.url,
      quality: 70,
      //   compressedWidth:400,
      success(res) {
        console.log("压缩成功，上传", res.tempFilePath)
        that.uploadServer(res.tempFilePath)
      },
      fail() {
        console.log("压缩失败，上传", file.url)
        that.uploadServer(file.url)
      }
    })
  },
  uploadServer(tempPhotoUrl) {
    var that = this;
    wx.showToast({ icon: "loading", title: "正在上传……" });
    that.setData({ uploading: true })
    request.upload('/car/user/uploadFile', tempPhotoUrl, function (res) {
      that.setData({ uploading: false })
      if (res.code == 0) {
        var evaImages = that.data.evaImages;
        evaImages.push(res.data);
        that.setData({ evaImages: evaImages })
        if (evaImages.length == that.data.originFiles.length) {
          that.saveEva();
        }
      } else {
        // wx.showModal({ title: '提示', content: '图片上传', showCancel: false });
        console.log("图片上传失败", res.message);
      }
    })
  },
  saveEva() {
    var that = this;
    var evaImages = '';
    for (var i = 0; i < this.data.evaImages.length; i++) {
      if (evaImages.length > 0) evaImages = evaImages + ",";
      evaImages = evaImages + this.data.evaImages[i];
    }
    var url = "/car/user/dealer/applyRefundAuctionMoney"
    var data = {
      auctionMoneyId: this.data.id,
      images: evaImages
    }
    request.post(url, data, function (res) {
      if (res.code == 0) {
        that.hasSava = true;
        var pages = getCurrentPages();
        if (pages.length > 1) {
          var prePage = pages[pages.length - 2];
          // 调用上个页面的方法 
          prePage.triggerChangeData()
        }

        wx.navigateBack({ delta: 1 });
      } else {
        wx.showModal({ title: '提示', content: res.message, showCancel: false });
      }
    })
  }

});
